local bianmei = fk.CreateSkill{
  name = "xiaobai__bianmei",
}

Fk:loadTranslationTable{
  ["xiaobai__bianmei"] = "辩眉",
  [":xiaobai__bianmei"] = "出牌阶段限一次，你可以与一名手牌比你多一张的角色拼点，赢者可以摸一张牌，或摸两张牌并令对方获得其拼点牌，若你赢，你可以再发动一次“辩眉”。",
  ["#xiaobai__bianmei_prompt"] = "你可以发动“辩眉”与一名手牌比你多一张的角色拼点",

  ["xiaobai__bianmei_askForDraw2"] = "辩眉：你可以点击“确定”摸两张牌并令 %dest 获得其拼点牌，或点击“取消”摸一张牌",
}

bianmei:addEffect("active", {
  anim_type = "control",
  prompt = "#xiaobai__bianmei_prompt",
  can_use = function (self, player)
    return player:usedSkillTimes(bianmei.name) < 1 and player:getHandcardNum() > 0 and
      table.find(Fk:currentRoom().alive_players, function (p)
        return p:getHandcardNum() - player:getHandcardNum() == 1
      end)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0  and to_select:getHandcardNum() - player:getHandcardNum() == 1
  end,  
  feasible = function (self, player, selected, selected_cardsss)
    return #selected == 1
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, bianmei.name)
    room:pindian(pindian)
    local winner = pindian.results[target].winner
    local loser = winner == player and target or player
    if winner then
      if winner.dead then return end
      if room:askToSkillInvoke(winner, {skill_name = bianmei.name, prompt = "xiaobai__bianmei_askForDraw2::"..loser.id}) then
        winner:drawCards(2, bianmei.name)
        if loser.dead then return end

        if winner == player then
          room:obtainCard(target, pindian.results[target].toCard, true, fk.ReasonPrey, player, bianmei.name)
        else
          room:obtainCard(player, pindian.fromCard, true, fk.ReasonPrey, player, bianmei.name)
        end
      else
        winner:drawCards(1 ,bianmei.name)
      end
      if player.dead then return end
      while player == winner do
        local done,data = room:askToUseActiveSkill(player, {skill_name = bianmei.name, prompt = "#xiaobai__bianmei_prompt"})
        if done and data then
          target = data.targets[1]
          pindian = player:pindian({target}, bianmei.name)
          winner = pindian.results[target].winner
          loser = (winner == player and target or player)
          if winner then
            if winner.dead then return end
            if room:askToSkillInvoke(winner,{ skill_name = bianmei.name, prompt = "xiaobai__bianmei_askForDraw2::"..loser.id}) then
              winner:drawCards(2, bianmei.name)
              if loser.dead then return end
              if winner == player then
                room:obtainCard(target, pindian.results[target].toCard, true, fk.ReasonPrey, player, bianmei.name)
              else
                room:obtainCard(player, pindian.fromCard, true, fk.ReasonPrey, player, bianmei.name)
              end
            else
              winner:drawCards(1 ,bianmei.name)
            end
            if player.dead then return end
          end
        else
          break
        end
      end
    end
  end
})

return bianmei